<template>
	<!-- 新建文件夹 -->
	<view class="cs-file-new-x">
		<view class="cs-file-new">
			<input class="cs-edit-input" type="text" v-model="folderName" maxlength="12" @input="changeInput" placeholder="请输入文件名(1~12个字)" placeholder-style="color:#C2C2C2" focus/>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				folderName: '' ,// 文件夹名称
				stuId: uni.getStorageSync('userInfo').id, //个人ID
				isSubmit: false,
				unid: '', //文件夹ID
			}
		},
		// 点击完成
		onNavigationBarButtonTap() {
			if (!this.folderName) {
				uni.showToast({
					title: '文件夹名不能为空',
					icon: 'none',
					duration: 2000
				})
				return
			}
			if (this.isSubmit) return // 防止重复点击
			this.isSubmit = true 
			var data = {
				usr_id: this.stuId,
				folder: this.folderName
			}
			var title = '添加成功';
			if(this.unid) {
				data.unid = this.unid;
				title = '修改成功';
			}
			this.$kbapi.apiPost('mobile/folder/save', data).then(res => {
				if (res.data.code == 200) {
					uni.showToast({title: title})
					// 1秒后进行后退
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				} else {
					uni.showToast({title: res.msg,icon: 'none'})
				}
			})
		},
		onLoad(e) {
			if(e.unid) {
				this.unid = e.unid;
				this.folderName = e.name;
				uni.setNavigationBarTitle({
					title: '修改文件夹'
				})
			}
		},
		methods: {
			/* 改变标题内容 */
			changeInput(e) {
				var num = e.target.value.length;
				if (num >= 12) {
					uni.showToast({
						title: '最多只能输入12个字',
						icon: 'none'
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #FAFAFA;
	}
	.cs-file-new-x {
	}
	.cs-file-new{
		padding: 20upx 30upx;
		background: #FFFFFF;
	}
	.cs-edit-input{
		font-size: 34upx;
		color: #333333;
	}
</style>
